package com.chucang.shucang.common.base.utils;

import com.github.benmanes.caffeine.cache.Cache;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * @author flitsneak
 * @email flitsneak@gmail.com
 * @date 2022/10/3 17:20
 * @description caffeine工具类
 */
@Component
@RequiredArgsConstructor
public class CaffeineUtil {
    private final Cache<String, Object> caffeineCache;

    /**
     * 添加缓存
     *
     * @param key   k
     * @param value value
     */
    public void setValue(String key, Object value) {
        caffeineCache.put(key, value);
    }


    /**
     * 获取缓存
     *
     * @param key k
     * @return obj
     */
    public Object getObject(String key) {
        return caffeineCache.getIfPresent(key);
    }

    /**
     * 删除缓存
     *
     * @param key k
     */
    public void remove(String key) {
        caffeineCache.asMap().remove(key);
    }
}
